const {
  querySql,
  updateSql,
  insertSql,
  deleteSql,
  queryOne,
} = require("../db");

function getRoomList(query) {
  const { roomName, roomCode, roomStatus, linkArea, pageNum, pageSize } = query;
  return querySql(
    `select * from classroom_list where roomName like '%${
      roomName || ""
    }%' and roomCode like '%${roomCode || ""}%' and roomStatus = ${
      roomStatus || 0
    } ${linkArea ? `and linkArea = '${linkArea}'` : ""} limit ${
      (pageNum - 1) * pageSize
    }, ${pageSize}`
  );
}

function getTotalCount(query) {
  const { roomName, roomCode, roomStatus, linkArea } = query;
  return queryOne(
    `SELECT COUNT(*) AS totalCount FROM classroom_list where roomName like '%${
      roomName || ""
    }%' and roomCode like '%${roomCode || ""}%' and roomStatus = ${
      roomStatus || 0
    } ${linkArea ? `and linkArea = '${linkArea}'` : ""}`
  );
}

function getRoomSelectData(searchContent) {
  return querySql(
    `select id,roomName,roomCode,space from classroom_list where roomName like '%${searchContent}%' or roomCode like '%${searchContent}%'`
  );
}

function editRoom(query) {
  return updateSql(query, "classroom_list", `where id = ${query.id}`);
}

function createRoom(query) {
  return insertSql(query, "classroom_list");
}

function deleteRoom(query) {
  return deleteSql("id", query.id, "classroom_list");
}

function getFreeClassroomList(sectionId) {
  return querySql(
    `select classroom_list.* , classSchedule_data.weekNumArr , classSchedule_data.linkSectionId
    FROM classroom_list 
    left join classSchedule_data on classSchedule_data.linkClassRoomId = classroom_list.id `
  );
}

module.exports = {
  getRoomList,
  getTotalCount,
  editRoom,
  createRoom,
  deleteRoom,
  getRoomSelectData,
  getFreeClassroomList,
};
